package com.beta.restapi.controller;

import com.beta.dto.pay.yeepay.YeepayNotifyDto;
import com.beta.restapi.internal.Consts;
import com.beta.restapi.internal.annotation.AuthSetting;
import com.beta.restapi.internal.annotation.AuthStrategy;
import com.beta.service.pay.IYeepayService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by vector on 2016/12/5.
 */
@RestController
@RequestMapping(Consts.API_PATH_PREFIX)
public class YeepayNotifyController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IYeepayService yeepayService;

    @AuthSetting(level = AuthStrategy.ANONYMOUS)
    @GetMapping(value = Consts.Resource.YEEPAY_PAY_NOTIFY)
    public Object handleServerTypePayNotify(HttpServletRequest request, HttpServletResponse response) {
        String data	= request.getParameter("data");
        logger.info("易宝支付 - 收到支付通知回调数据：data={}", data);

        if(data == null) {
            return "SUCCESS";
        }

        YeepayNotifyDto notifyDto = yeepayService.executeHandlePayNotifyData(data);
        switch (notifyDto.getNotifyType()) {
            case REDIRECT:
                // TODO handleResirect
                return "";
            case SERVER:
                return "SUCCESS";
            default:
                return "";
        }
    }

    @AuthSetting(level = AuthStrategy.ANONYMOUS)
    @GetMapping(value = Consts.Resource.YEEPAY_DIVIDE_NOTIFY)
    public Object handleDivideNotify(HttpServletRequest request, HttpServletResponse response) {
        String data	= request.getParameter("data");
        logger.info("易宝支付 - 分账 ==> 回调通知数据：data={}", data);
        if (StringUtils.isEmpty(data)) {
            return "data can't be null";
        }

        yeepayService.executeHandleDivideNotify(data);
        return "SUCCESS";
    }

}
